import { instance } from "../axios";

// 获取用户列表
export const getUsers = async () => {
  const response = await instance.get('/admin/users');
  return response; // instance已经在拦截器中处理了response.data
};

// 获取单个用户详情
export const getUserById = (userId) => {
  return instance.get(`/admin/users/${userId}`);
};

// 封禁用户
export const banUser = (userId, reason, banDurationDays) => {
  return instance.post(`/admin/users/${userId}/ban`, null, {
    params: { reason, banDurationDays }
  });
};

// 解封用户
export const unbanUser = (userId) => {
  return instance.post(`/admin/users/${userId}/unban`);
};

// 处理举报
export const processReport = (reportId, processResult, actionTaken) => {
  return instance.post('/admin/report/process', null, {
    params: { reportId, processResult, actionTaken }
  });
};

// 创建限免活动
export const createPromotion = (torrentId, startTime, endTime) => {
  return instance.post('/admin/promotions', null, {
    params: { torrentId, startTime, endTime }
  });
};

// 关闭限免活动
export const closePromotion = (torrentId) => {
  return instance.post('/admin/del-promotions', null, {
    params: { torrentId }
  });
};

// 获取当前活动的限免
export const getActivePromotions = () => {
  return instance.get('/admin/promotions/active');
};

// 添加商品
export const addProduct = (name, description, price, type, duration = 0, stock = -1) => {
  return instance.post('/admin/store', null, {
    params: { name, description, price, type, duration, stock }
  });
};

// 获取所有商品
export const getAllProducts = () => {
  return instance.get('/admin/store');
};

// 更新商品
export const updateProduct = (id, name, description, price, type, duration, stock) => {
  const params = {};
  if (name) params.name = name;
  if (description) params.description = description;
  if (price !== undefined) params.price = price;
  if (type) params.type = type;
  if (duration !== undefined) params.duration = duration;
  if (stock !== undefined) params.stock = stock;

  return instance.put(`/admin/store/${id}`, null, { params });
};

// 删除商品
export const deleteProduct = (id) => {
  return instance.delete(`/admin/store/${id}`);
};

// 获取待处理的举报
export const getPendingReports = () => {
  // 尝试使用管理员接口获取举报
  return instance.get('/report/pending');
};

// 获取待审核的种子
export const getPendingTorrents = () => {
  return instance.get('/admin/torrents/pending');
};

// 审核种子
export const reviewTorrent = (torrentId, status, reason = null) => {
  const params = { torrentId, status };
  if (reason) params.reason = reason;
  return instance.post('/admin/torrents/review', null, { params });
};

// 发布通知
export const publishNotice = (adminId, topic, content) => {
  return instance.post('/admin/notice', null, {
    params: { adminId, topic, content }
  });
};

// 获取所有通知
export const getAllNotices = () => {
  return instance.get('/admin/notice');
};

// 删除通知
export const deleteNotice = (noticeId) => {
  return instance.delete(`/admin/notice/${noticeId}`);
};

// 获取系统参数
export const getSystemParam = () => {
  return instance.get('/admin/config-select');
};

// 配置系统参数
export const configureSystemParam = (configKey, configValue) => {
  return instance.post('/admin/config-setting', null, {
    params: { configKey, configValue }
  });
};